//
//  TeamRankingDetailView.m
//  RedballBar
//
//  Created by Faith  on 2024/9/18.
//

#import "TeamRankingDetailView.h"
#import "TeamRankingCategoryView.h"

@interface TeamRankingDetailView() <UICollectionViewDelegate, UICollectionViewDataSource>

/// 头部控件
@property (nonatomic, strong) UIView *headerView;


/// 头部控件
@property (nonatomic, strong) TeamRankingCategoryView *categoryView;

@end


@implementation TeamRankingDetailView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.layer.maskedCorners = kCALayerMaxXMinYCorner | kCALayerMaxXMaxYCorner;
        
        // 头部控件
        [self addSubview:self.categoryView];
        [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.width.mas_equalTo(SIZE(100));
            make.bottom.equalTo(self);
        }];
        
        // 头部控件
        [self addSubview:self.headerView];
        [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self.categoryView.mas_right);
            make.right.equalTo(self);
        }];
        
        // 列表控件
        [self addSubview:self.contentView];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.equalTo(self.headerView);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
        }];
        
        _contentView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            
            [self.dataList addObjectsFromArray:@[
                @{
                    @"rank": @"1",
                    @"avatar": @"https://wx2.sinaimg.cn/orj360/0068mQp7gy1htnn0317qlj63b04eox6u02.jpg",
                    @"name": @"德米特里·尤索夫",
                    @"totalNumber": @"12"
                },
                @{
                    @"rank": @"2",
                    @"avatar": @"https://wx1.sinaimg.cn/orj360/0068mQp7gy1htnn2n6n5uj63b04eob2i02.jpg",
                    @"name": @"安德里·特卡楚克安德里·特卡楚克安德里·特卡楚克安德里·特卡楚克安德里·特卡楚克安德里·特卡楚克",
                    @"totalNumber": @"9"
                },
                @{
                    @"rank": @"3",
                    @"avatar": @"https://wx1.sinaimg.cn/orj360/0068mQp7gy1htnnkqeynxj63b04eonpf02.jpg",
                    @"name": @"贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽贝卡·瓦奇贝拉泽",
                    @"totalNumber": @"7"
                },
            ]];
            
            [_contentView.mj_footer endRefreshing];
            [_contentView reloadData];
        }];
    }
    return self;
}

/// 懒加载头部视图
- (UIView *)headerView {
    if(!_headerView) {
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = [UIColor colorWithHexString:@"#F3F3F3"];
    }
    return _headerView;
}

/// 懒加载列表控件
- (UICollectionView *)contentView {
    if(!_contentView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
        layout.minimumInteritemSpacing = 0; // 每列之间的最小间距
        layout.minimumLineSpacing = 0; // 每行之间的最小间距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 上下左右间距
        _contentView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _contentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _contentView.backgroundColor = [UIColor clearColor];
        _contentView.delegate = self;
        _contentView.dataSource = self;
        _contentView.showsVerticalScrollIndicator = NO;
        _contentView.alwaysBounceVertical = YES;
        [_contentView registerClass:[TeamRankingDetailCell class] forCellWithReuseIdentifier:@"kTeamRankingDetailCell"];
    }
    return _contentView;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TeamRankingDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kTeamRankingDetailCell" forIndexPath:indexPath];
    [cell setData:self.dataList[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat collectionViewWidth = CGRectGetWidth(collectionView.frame);
    CGFloat sectionInsetLeft = ((UICollectionViewFlowLayout *)collectionViewLayout).sectionInset.left;
    CGFloat sectionInsetRight = ((UICollectionViewFlowLayout *)collectionViewLayout).sectionInset.right;
    CGFloat totalSpacing = sectionInsetLeft + sectionInsetRight;
    CGFloat itemWidth = (collectionViewWidth - totalSpacing);
    return CGSizeMake(itemWidth, 1);
}

/// 设置数据
- (void)setData:(NSArray*)headerList dataList:(NSArray*)dataList {
    while(self.headerView.subviews.count!=0) {
        [self.headerView.subviews[0] removeFromSuperview];
    }
    UIView *lastView = nil;
    for(int i=0;i<headerList.count;i++) {
        // 标题文字
        UILabel *headerItem = [[UILabel alloc] init];
        headerItem.textColor = [UIColor colorWithHexString:@"#5A5A5A"];
        headerItem.font = [UIFont systemFontOfSize:FONT_SIZE(10) weight:UIFontWeightMedium];
        headerItem.text = headerList[i][@"title"];
        if(i==0 || i==2) {
            headerItem.textAlignment = NSTextAlignmentCenter;
        }
        [self.headerView addSubview:headerItem];
        
        CGFloat width = 0;
        if(i==0 || i==2) {
            width = SIZE(50);
        }
        [headerItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView).offset(SIZE(10));
            if(i==0) {
                make.left.equalTo(self.headerView);
            } else {
                make.left.equalTo(lastView.mas_right);
            }
            make.bottom.equalTo(self.headerView).offset(-SIZE(10));
            if(width!=0) {
                make.width.mas_equalTo(width);
            }
            if(i==headerList.count - 1) {
                make.right.equalTo(self.headerView);
            }
        }];
        lastView = headerItem;
    }
    
    self.dataList = [[NSMutableArray alloc] initWithArray:dataList];
    [self.contentView reloadData];
}

@end




@interface TeamRankingDetailCell()

/// 排名
@property (nonatomic, strong) UILabel *rankView;

/// 球员信息
@property (nonatomic, strong) UIView *playerInfo;

/// 球员LOGO
@property (nonatomic, strong) UIImageView *playerInfoLogo;

/// 球员姓名
@property (nonatomic, strong) UILabel *playerInfoName;

/// 总数
@property (nonatomic, strong) UILabel *totalNumberView;

/// 分割线
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation TeamRankingDetailCell

- (instancetype)initWithFrame:(CGRect)frame {
    id instance = [super initWithFrame:frame];
    [self layoutItems];
    return instance;
}

- (void)layoutItems {
    UIView *contentView = [[UIView alloc] init];
    [self.contentView addSubview:contentView];
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
        make.width.mas_equalTo(self.contentView.frame.size.width);
    }];
    
    // 第一个值
    _rankView = [[UILabel alloc] init];
    _rankView.numberOfLines = 1;
    _rankView.font = [UIFont systemFontOfSize:FONT_SIZE(11) weight:UIFontWeightRegular];
    _rankView.textAlignment = NSTextAlignmentCenter;
    _rankView.textColor = [UIColor colorWithHexString:@"#5A5A5A"];
    [contentView addSubview:_rankView];
    [_rankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView);
        make.left.equalTo(contentView);
        make.width.mas_equalTo(SIZE(50));
    }];
    
    // 球员信息
    _playerInfo = [[UIView alloc] init];
    [contentView addSubview:_playerInfo];
    [_playerInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rankView).offset(SIZE(5));
        make.left.equalTo(_rankView.mas_right);
        make.bottom.equalTo(contentView).offset(-SIZE(5));
    }];
    
    // 球员LOGO
    _playerInfoLogo = [[UIImageView alloc] init];
    _playerInfoLogo.layer.cornerRadius = SIZE(20) / 2;
    _playerInfoLogo.layer.masksToBounds = YES;
    [_playerInfo addSubview:_playerInfoLogo];
    [_playerInfoLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playerInfo);
        make.size.mas_equalTo(SIZE(20));
        make.centerY.equalTo(_playerInfo); // 始终垂直居中
        // 设置 playerInfoName 的顶部和底部约束
        make.top.greaterThanOrEqualTo(_playerInfo); // 顶部约束
        make.bottom.lessThanOrEqualTo(_playerInfo); // 底部约束
    }];
    
    // 球员姓名
    _playerInfoName = [[UILabel alloc] init];
    _playerInfoName.numberOfLines = 0; // 允许多行
    _playerInfoName.font = [UIFont systemFontOfSize:FONT_SIZE(9) weight:UIFontWeightRegular];
    _playerInfoName.textColor = [UIColor colorWithHexString:@"#000000"];
    [_playerInfo addSubview:_playerInfoName];
    
    // 设置 playerInfoName 的约束
    [_playerInfoName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playerInfoLogo.mas_right).offset(SIZE(6));
        make.right.equalTo(_playerInfo); // 右边对齐 _playerInfo
        // 设置 playerInfoName 的顶部和底部约束
        make.top.greaterThanOrEqualTo(_playerInfo); // 顶部约束
        make.bottom.lessThanOrEqualTo(_playerInfo); // 底部约束
        // 通过优先级让 playerInfoName 尽量与 playerInfoLogo 垂直居中
        make.centerY.equalTo(_playerInfoLogo).priorityLow(); // 优先与 logo 垂直居中
    }];
    
    // 总数
    _totalNumberView = [[UILabel alloc] init];
    _totalNumberView.numberOfLines = 1;
    _totalNumberView.font = [UIFont systemFontOfSize:FONT_SIZE(11) weight:UIFontWeightRegular];
    _totalNumberView.textAlignment = NSTextAlignmentCenter;
    _totalNumberView.textColor = [UIColor colorWithHexString:@"#5A5A5A"];
    [contentView addSubview:_totalNumberView];
    [_totalNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView);
        make.left.equalTo(_playerInfo.mas_right);
        make.width.mas_equalTo(SIZE(50));
        make.right.equalTo(contentView);
        make.bottom.equalTo(contentView);
    }];
    
    // 底部线条
    _bottomLine = [[UIView alloc] init];
    _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#DEDEDE"];
    [contentView addSubview:_bottomLine];
    [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rankView.mas_bottom);
        make.left.equalTo(contentView);
        make.right.equalTo(contentView);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(contentView);
    }];
}

/// 设置数据
- (void)setData:(NSDictionary*)data {
    _rankView.text = [NSString stringWithFormat:@"%@", data[@"rank"]];
    [_playerInfoLogo sd_setImageWithURL:[NSURL URLWithString:data[@"avatar"]]];
    _playerInfoName.text = [NSString stringWithFormat:@"%@", data[@"name"]];
    _totalNumberView.text = [NSString stringWithFormat:@"%@", data[@"totalNumber"]];
    [self layoutIfNeeded];
}

@end
